<?php

namespace App\Listeners;

use App\Events\AsynchronousIOFile;
use App\Models\Branch;
use App\Http\Controllers\BranchUtils;

class ListenAsynchronous
{
    public function handle(AsynchronousIOFile $asynchronousIOFile) {
        $types = $asynchronousIOFile->types;
        $user_id = $asynchronousIOFile->user_id;
        $worker_id = $asynchronousIOFile->worker_id;
        $start_time = $asynchronousIOFile->start_time;
        $end_time = $asynchronousIOFile->end_time;
        $time_chose = $asynchronousIOFile->time_chose;
        $branch_list = $asynchronousIOFile->branch_id;
        $excel_record_id = $asynchronousIOFile->excel_record_id;
        if ($types == 1) {
            $product_id = $asynchronousIOFile->product_id;
            $order_status = $asynchronousIOFile->order_status;
            $where = [];
            if ($product_id) {
                $where[] = ['product_id', $product_id];
            }
            if ($user_id) {
                $where[] = ['user_id', $user_id];
            }
            if ($worker_id) {
                $where[] = ['worker_id', $worker_id];
            }
            if ($start_time != 0 && $end_time != 0) {
                if ($time_chose == 1) {
                    $where[] = ['valid_time', '>=', $start_time];
                    $where[] = ['valid_time', '<=', $end_time];
                } elseif($time_chose == 2) {
                    $where[] = ['sign_time', '>=', $start_time];
                    $where[] = ['sign_time', '<=', $end_time];
                } else {
                    $where[] = ['contract_end_date', '>=', $start_time];
                    $where[] = ['contract_end_date', '<=', $end_time];
                }
            }
            if (env('COMPANY') == 2 || env('COMPANY') == 1) {
                OrderUtils::getOtherOrderRecode($where, $order_status, $time_chose, $excel_record_id, $branch_list);
            } elseif (env('COMPANY') == 3) {
                OrderUtils::getGYOrderRecode($where, $order_status, $excel_record_id, $branch_list);
            }
        } elseif ($types == 2) {
            $contract_no = $asynchronousIOFile->product_id;
            $status = $asynchronousIOFile->order_status;
            $where = [];
            if ($user_id) {
                $where[] = ['order.user_id', $user_id];
            }
            if ($worker_id) {
                $where[] = ['order.worker_id', $worker_id];
            }
            if ($start_time != 0 && $end_time != 0) {
                if ($time_chose == 1) {
                    $where[] = ['date', '>=', $start_time];
                    $where[] = ['date', '<=', $end_time];
                } elseif ($time_chose == 2) {
                    $where[] = ['order.sign_time', '>=', $start_time];
                    $where[] = ['order.sign_time', '<=', $end_time];
                }
            }
            if ($contract_no) {
                $where[] = ['order.contract_no', $contract_no];
            }
            if (env('COMPANY') == 2 || env('COMPANY') == 1) {
                RecordUtils::getOtherRecode($status, $where, $excel_record_id, $branch_list);
            } elseif (env('COMPANY') == 3) {
                RecordUtils::getGYRecode($status, $where, $excel_record_id, $branch_list);
            }
        } elseif ($types == 3) {
            $tag_list = $asynchronousIOFile->order_status;
            UserListUtils::GetUserList($worker_id, $branch_list, $excel_record_id, $tag_list);
        }
    }
}
